<template>
  <div class="prescription-search">
    <el-card shadow="hover" class="search-card">
      <h2>希望站验方查询</h2>

      <!-- 搜索栏 -->
      <el-input
          v-model="diseaseName"
          placeholder="请输入病症名称"
          @keyup.enter="searchPrescriptions"
          style="margin-bottom: 20px;"
      >
        <template #append>
          <el-button type="primary" @click="searchPrescriptions">查询</el-button>
        </template>
      </el-input>

      <!-- 加载提示 -->
      <div v-if="loading" class="loading-message">
        正在查询，请稍候...
      </div>

      <!-- 错误提示 -->
      <div v-if="error" class="error-message">
        {{ error }}
      </div>

      <!-- 搜索结果 -->
      <transition name="fade">
        <div v-show="prescriptions.length > 0 && !loading" class="search-results">
          <p>已为您搜索到 {{ prescriptions.length }} 条验方</p>
          <ul>
            <li v-for="(prescription, index) in prescriptions" :key="index" class="prescription-item">
              <h3>{{ prescription.name }}</h3>
              <p><strong>[方剂]</strong> {{ prescription.formula }}</p>
              <p><strong>[制法]</strong> {{ prescription.preparation }}</p>
              <p><strong>[功效]</strong> {{ prescription.efficacy }}</p>
              <p><strong>[验证]</strong> {{ prescription.validation }}</p>
            </li>
          </ul>
          <el-button type="default" @click="clearResults" style="margin-top: 10px;">清空结果</el-button>
        </div>
      </transition>

      <!-- 无结果提示 -->
      <div v-if="!loading && prescriptions.length === 0 && diseaseName">
        <p>未找到相关验方，请检查输入或尝试其他关键词。</p>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'PrescriptionSearch',
  data() {
    return {
      diseaseName: '',
      prescriptions: [],
      loading: false,
      error: ''
    };
  },
  methods: {
    async searchPrescriptions() {
      const diseaseName = this.diseaseName.trim();

      console.log("搜索病症名称:", diseaseName);

      if (!diseaseName) {
        this.error = "请输入病症名称";
        return;
      }

      this.loading = true;
      this.error = '';

      try {
        const res = await this.$axios.get('/api/prescriptions/search', {
          params: { diseaseName }
        });

        console.log('返回的数据:', res.data);

        if (res.data.error) {
          this.error = res.data.error;
          this.prescriptions = [];
        } else {
          this.prescriptions = res.data;
        }

      } catch (error) {
        console.error('请求失败:', error);

        if (error.response && error.response.status === 404) {
          this.error = '接口未找到，请检查后端是否运行';
        } else if (error.message.includes('Network Error')) {
          this.error = '网络错误，请检查后端服务是否启动';
        } else {
          this.error = '查询失败，请稍后再试';
        }
      } finally {
        this.loading = false;
      }
    },
    clearResults() {
      this.prescriptions = [];
      this.diseaseName = '';
    }
  }
};
</script>

<style scoped>
.prescription-search {
  max-width: 1000px;
  margin: 40px auto;
  padding: 20px;
}

.search-card {
  padding: 20px;
  border-radius: 10px;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, .1);
}

.loading-message, .error-message {
  text-align: center;
  font-size: 16px;
  color: #606266;
  margin-top: 20px;
}

.error-message {
  color: #F56C6C;
}

.search-results {
  margin-top: 20px;
}

.search-results ul {
  list-style-type: none;
  padding: 0;
}

.prescription-item {
  background-color: #f3f3f3;
  border: 1px solid #eee;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 15px;
  transition: box-shadow 0.3s ease;
}

.prescription-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 动画效果 */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s ease;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>